package com.example.wechatrecords;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity2 extends AppCompatActivity {

    private static final String TAG = "MainActivity2";
    private static final int IMAGE_REQUEST_CODE = 200;

    private ImageView mImgPhoto;
    private TextView mTvName;
    private TextView mTvPhoto;
    private String mPicturePath;
    private View mNameView;
    private View mPhotoView;
    private AlertDialog.Builder mDialog;
    private AlertDialog.Builder mPhotoDialog;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //注意要清除 FLAG_TRANSLUCENT_STATUS flag
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        setContentView(R.layout.activity_main2);

        ActivityCompat.requestPermissions(this,new String[] {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                }, 0);

        initView();
        initListener();

    }

    private void initListener() {
        mImgPhoto.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, IMAGE_REQUEST_CODE);
        });

        mTvName.setOnClickListener(v -> {
            if (mNameView == null) {
                mNameView = LayoutInflater.from(MainActivity2.this).inflate(R.layout.dialog_name_update, null,false);
            }
            EditText edtName = mNameView.findViewById(R.id.edt_update_name);

            if (mDialog == null) {
                mDialog = new AlertDialog.Builder(MainActivity2.this);
                mDialog.setView(mNameView);
                mDialog.setPositiveButton("确定", (dialog1, which) -> {
                    String name = edtName.getText().toString().trim();
                    mTvName.setText(name);
                    dialog1.dismiss();
                });
                mDialog.setNegativeButton("取消",null);
            }
            mDialog.show();
        });

        mTvPhoto.setOnClickListener(v -> {
            if (mPhotoView == null) {
                mPhotoView = LayoutInflater.from(MainActivity2.this).inflate(R.layout.dialog_phone_update, null,false);
            }
            EditText edtPhone = mPhotoView.findViewById(R.id.edt_update_phone);

            if (mPhotoDialog == null) {
                mPhotoDialog = new AlertDialog.Builder(MainActivity2.this);
                mPhotoDialog.setView(mPhotoView);
                mPhotoDialog.setPositiveButton("确定", (dialog1, which) -> {
                    String phone = edtPhone.getText().toString().trim();
                    mTvPhoto.setText(phone);
                    dialog1.dismiss();
                });
                mPhotoDialog.setNegativeButton("取消",null);
            }
            mPhotoDialog.show();
        });
    }

    private void initView() {
        mImgPhoto = findViewById(R.id.img_photo);
        mTvName = findViewById(R.id.tv_name);
        mTvPhoto = findViewById(R.id.tv_phone);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mPicturePath = cursor.getString(columnIndex);
            cursor.close();
            mImgPhoto.setImageBitmap(BitmapFactory.decodeFile(mPicturePath));
        }
    }
}